package com.gxa.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.base.RespResult;
import com.gxa.service.entity.ServiceSlabel;
import com.gxa.service.service.ServiceSlabelService;
import com.gxa.service.mapper.ServiceSlabelMapper;
import com.gxa.service.vo.AllLabelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class ServiceSlabelServiceImpl extends ServiceImpl<ServiceSlabelMapper, ServiceSlabel>
    implements ServiceSlabelService{

    @Autowired
    private ServiceSlabelMapper serviceSlabelMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public RespResult findAllLabel(Integer id) {
        String labelRedis="findAllLabel"+id;
        //获取到redisTemplate对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //序列化redisTemplate模板
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        List<AllLabelVo> label= (List<AllLabelVo>) valueOperations.get(labelRedis);
        if(label==null) {
            label = serviceSlabelMapper.findAllLabel(id);
            valueOperations.set(labelRedis,label,5, TimeUnit.MINUTES);
        }
        return RespResult.success(label);
    }
}




